<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
    >
      <!-- 预览的图片位置 -->
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <!-- +号 -->
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <!-- 进度条 -->
      <el-progress v-if="flag" class="progress" type="circle" :percentage="percent" />
    </el-upload>

  </div>
</template>

<script>
// 1. 导入Cos对象
const COS = require('cos-js-sdk-v5')
// 2. 配置秘钥实例化cos对象
const cos = new COS({
  SecretId: 'AKIDc3ECvNdfU5SYOyNZYJ0TiiU3OULyMVP4', // 身份识别ID
  SecretKey: 'ThgQwDF2ZpoWy93f0A3f05uTmqeCVZOx' // 身份秘钥
})
export default {
  name: 'UploadImage',
  props: {
    // url: {
    //   type: String,
    //   default: ''
    // }
    // 模块优化
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      imageUrl: '',
      percent: 0,
      flag: false
    }
  },
  watch: {
    // url: function() {
    //   this.imageUrl = this.url
    // }
    // 模块优化
    value: function() {
      this.imageUrl = this.value
    }
  },
  methods: {
    upload({ file }) {
      // console.log(file)
      // 显示进度条
      this.flag = true
      cos.putObject({
        Bucket: 'bucketcy-1308732230', /* 存储桶 */
        Region: 'ap-beijing',
        Key: file.name, /* 文件名 */
        StorageClass: 'STANDARD', // 上传模式, 标准模式
        Body: file, // 上传文件对象
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        // data：代表上传成功的对象
        // err: 发生错误时的对象
        console.log(data, err)
        if (data.statusCode === 200) {
          this.imageUrl = `https:${data.Location}`
          // 子组件自定义事件,把url传出去
          // this.$emit('get-url', this.imageUrl)
          // 模块优化
          this.$emit('input', this.imageUrl)
          // 关闭进度条
          this.flag = false
        }
      })
    },
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPNG) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPNG && isLt2M
    }
  }

}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.progress {
  position: absolute;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #ffff;
  height: 100%;
  width: 100%;
  justify-content: center;
  align-items: center;
}
</style>
